package com.rybbaby.tss.front.tssTransitionFeesSet.service.imp;

import com.rybbaby.tss.core.utils.SequenceUtil;
import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.core.web.service.imp.BaseServiceImp;
import com.rybbaby.tss.front.tssTransitionFeesSet.dao.ITssTransitionFeesSetDao;
import com.rybbaby.tss.front.tssTransitionFeesSet.entity.TssTransitionFeesSet;
import com.rybbaby.tss.front.tssTransitionFeesSet.entity.TssTransitionFeesSetGroup;
import com.rybbaby.tss.front.tssTransitionFeesSet.service.ITssTransitionFeesSetGroupService;
import com.rybbaby.tss.front.tssTransitionFeesSet.service.ITssTransitionFeesSetService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 雷春琼
 * @description 过渡收费设置数据库操作服务实现
 * @createDate 2017-08-10 上午12:43:26
 */
@Service
@Transactional
public class TssTransitionFeesSetServiceImp extends BaseServiceImp<TssTransitionFeesSet> implements ITssTransitionFeesSetService {


    @Autowired
    private ITssTransitionFeesSetDao dao;
    @Autowired
    private ITssTransitionFeesSetGroupService feesSetGroupService;


    /**
     * 根据状态来修改过渡收费信息
     *
     * @param targetDateId
     * @param isBlockUp
     * @return
     */
    @Override
    public Integer updateTransitionFeeSetByIsBlockUp(String targetDateId, Boolean isBlockUp) {
        return dao.updateTransitionFeeSetByIsBlockUp(targetDateId, isBlockUp);
    }

    /**
     * 根据状态来修改过渡收费信息
     *
     * @param targetDateId
     * @param isBlockUp
     * @return
     */
    @Override
    public Integer updateTransitionsFeeSetByIsBlockUp(String targetDateId, Boolean isBlockUp) {
        return dao.updateTransitionsFeeSetByIsBlockUp(targetDateId, isBlockUp);
    }

    /**
     * 新增编辑过渡收费
     *
     * @param map
     * @param entity
     */
    @Override
    public Boolean saveOrUpdate(Map<String, Object> map, TssTransitionFeesSet entity) throws Exception {
        boolean bool = true;
        List<TssTransitionFeesSetGroup> feesSetGroupList = new ArrayList<>();
        String id = entity.getId();
        if (StringUtils.isNotBlank(id)) {
            TssTransitionFeesSet destEntity = this.get(id);
            BeanUtils.copyProperties(entity, destEntity, "createTime","isBlockUp");
            destEntity.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
            this.updateEntitiy(destEntity);
            //半日
            TssTransitionFeesSetGroup feesSetHalf = new TssTransitionFeesSetGroup();
            List<TssTransitionFeesSetGroup> groupListHalf = feesSetGroupService.getListByFeesSetIdAndType(id, 2);
            feesSetHalf.setMealFeeLimitEnd(Double.parseDouble(map.get("mealFeeLimitEndHalfDay").toString()));
            feesSetHalf.setMealFeeLimitStart(Double.parseDouble(map.get("mealFeeLimitStartHalfDay").toString()));
            feesSetHalf.setMealFeeStandard(Double.parseDouble(map.get("mealFeeStandardHalfDay").toString()));
            feesSetHalf.setOneTimeItemsFee(Double.parseDouble(map.get("oneTimeItemsFeeHalfADay").toString()));
            feesSetHalf.setType(groupListHalf.get(0).getType());
            feesSetHalf.setId(groupListHalf.get(0).getId());
            feesSetHalf.setFeesSetId(groupListHalf.get(0).getFeesSetId());
            TssTransitionFeesSetGroup destFeesSetHalf = this.feesSetGroupService.get(groupListHalf.get(0).getId());
            BeanUtils.copyProperties(feesSetHalf, destFeesSetHalf);
            this.feesSetGroupService.updateEntitiy(destFeesSetHalf);

            //全日
            List<TssTransitionFeesSetGroup> groupListfull = feesSetGroupService.getListByFeesSetIdAndType(id, 1);

            TssTransitionFeesSetGroup feesSetFull = new TssTransitionFeesSetGroup();
            feesSetFull.setId(groupListfull.get(0).getId());
            feesSetFull.setFeesSetId(id);
            feesSetFull.setVersion(groupListfull.get(0).getVersion());
            feesSetFull.setType(1);
            feesSetFull.setMealFeeLimitEnd(Double.parseDouble(map.get("mealFeeLimitEndFullDay").toString()));
            feesSetFull.setMealFeeLimitStart(Double.parseDouble(map.get("mealFeeLimitStartFullDay").toString()));
            feesSetFull.setMealFeeStandard(Double.parseDouble(map.get("mealFeeStandardFullDay").toString()));
            feesSetFull.setOneTimeItemsFee(Double.parseDouble(map.get("oneTimeItemsFeeFullDay").toString()));
            TssTransitionFeesSetGroup destFeesSetFull = this.feesSetGroupService.get(groupListfull.get(0).getId());
            BeanUtils.copyProperties(feesSetFull, destFeesSetFull);
            this.feesSetGroupService.updateEntitiy(destFeesSetFull);
        } else {
            entity.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis()));
            entity.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            Serializable newId = this.save(entity);
            //半日
            TssTransitionFeesSetGroup feesSetHalf = new TssTransitionFeesSetGroup();
            feesSetHalf.setId(SequenceUtil.getLowerCaseUUID());
            feesSetHalf.setFeesSetId(newId.toString());
            feesSetHalf.setType(2);
            feesSetHalf.setMealFeeLimitEnd(Double.parseDouble(map.get("mealFeeLimitEndHalfDay").toString()));
            feesSetHalf.setMealFeeLimitStart(Double.parseDouble(map.get("mealFeeLimitStartHalfDay").toString()));
            feesSetHalf.setMealFeeStandard(Double.parseDouble(map.get("mealFeeStandardHalfDay").toString()));
            feesSetHalf.setOneTimeItemsFee(Double.parseDouble(map.get("oneTimeItemsFeeHalfADay").toString()));
            //全日
            TssTransitionFeesSetGroup feesSetFull = new TssTransitionFeesSetGroup();
            feesSetHalf.setId(SequenceUtil.getLowerCaseUUID());
            feesSetFull.setFeesSetId(newId.toString());
            feesSetFull.setType(1);
            feesSetFull.setMealFeeLimitEnd(Double.parseDouble(map.get("mealFeeLimitEndFullDay").toString()));
            feesSetFull.setMealFeeLimitStart(Double.parseDouble(map.get("mealFeeLimitStartFullDay").toString()));
            feesSetFull.setMealFeeStandard(Double.parseDouble(map.get("mealFeeStandardFullDay").toString()));
            feesSetFull.setOneTimeItemsFee(Double.parseDouble(map.get("oneTimeItemsFeeFullDay").toString()));
            feesSetGroupList.add(feesSetHalf);
            feesSetGroupList.add(feesSetFull);
            this.feesSetGroupService.save(feesSetHalf);
            this.feesSetGroupService.save(feesSetFull);
            if (!bool) {
                return !bool;
            }
            if (newId == null) {
                return !bool;
            }
        }
        return bool;
    }


}
